<%@ page language="java" contentType="text/html; charset=UTF-8"%>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
System.out.println(basePath);
%>

<script type="text/javascript">
var selContinent = {};
var selCountry = {};

function displayContinent(label, continentId, countryId, cityId) {
	var url = "<%=basePath%>flight/flightQueryAction!getContinent.action?r=" + getRandomString();
	//alert("url is:" + url);
	new Ajax.Request(url, {
		method: "get",
		onComplete: function(transport) {
			var ret = transport.responseText.trim();
			//alert("get displayContinent response text is:" + ret);
			populateContinentCtl(label, ret, continentId);
			displayCountry(label, selContinent[label], countryId, cityId);
		}
	});
}

function populateContinentCtl(label, strResponse) {
	var selectedCountinentId = (arguments.length == 2) ? arguments[1] : "";
	var kvArr = strResponse.split("|");
	var continentCtl = $(label + "_continent");
	var options = continentCtl.options;
	var selectedIndex = 0;
	var length = kvArr.length;
	for(var i = 0; i < length; i ++) {
		var kv = kvArr[i].trim();
		if(kv != "") {
			options[i] = new Option(kv, kv);
			if(i === 0) {
				selContinent[label] = kv;
			}
			if(selectedCountinentId == kv) {
				selContinent[label] = kv;
				selectedIndex = i;
			}
		}
	}
	continentCtl.selectedIndex = selectedIndex;
	continentCtl.disabled = false;
}

function displayCountry(label, continentId, countryId, cityId) {
	removeAndDisableSelect($(label + "_country"));
	var url = "<%=basePath%>flight/flightQueryAction!getCountry.action?continent=" + encodeURIComponent(continentId) + "&r=" + getRandomString();
	//alert("get country url is:" + url);
	new Ajax.Request(url, {
		method: "get",
		onComplete: function(transport) {
			var ret = transport.responseText.trim();
			//alert("get country response text is:" + ret);
			populateCountryCtl(label, ret, countryId);
			displayAirport(label, selCountry[label], cityId);
		}
	});
}

function populateCountryCtl(label, strResponse, countryId) {
	var selectedCountryId = (arguments.length == 2) ? arguments[1] : "";
	var kvArr = strResponse.split("|");
	var countryCtl = $(label + "_country");
	var options = countryCtl.options;
	var selectedIndex = 0;
	for(var i = 0; i < kvArr.length; i ++) {
		var kv = kvArr[i];
		if(kv.trim() != "") {
			var kvmap = kv.split(":");
			options[i] = new Option(kvmap[0].trim(), kvmap[1].trim());
			if(i == 0) {
				selCountry[label] = kvmap[1].trim();
			}
			if(kvmap[1] && selectedCountryId == kvmap[1].trim()) {
				selCountry[label] = kvmap[1].trim();
				selectedIndex = i;
			}
		}
	}
	countryCtl.selectedIndex = selectedIndex;
	countryCtl.disabled = false;
}


function displayAirport(label, countryId, cityId) {
	removeAndDisableSelect($(label + "_airport"));
	var url = "<%=basePath%>flight/flightQueryAction!getAirport.action?country=" + countryId + "&r=" + getRandomString();
	//alert(url);
	new Ajax.Request(url, {
		method: "get",
		onComplete: function(transport) {
			var ret = transport.responseText.trim();
			populateAirportCtl(label, ret, cityId);
		}
	});
}

function populateAirportCtl(label, strResponse) {
	var selectedCityId = (arguments.length == 2) ? arguments[1] : "";
	
		var cityCtl = $(label + "_airport");
		var options = cityCtl.options;
		var length = options.length;
		for(var i = 0; i < length; i ++) {
			cityCtl.remove(0);
		}
		var selectedIndex = -1;
		if(strResponse != null) {
			var kvArr = strResponse.split("|");
			for(var i = 0; i < kvArr.length; i ++) {
				var kv = kvArr[i];
				if(kv.trim() != "") {
					var kvmap = kv.split(":");
					options[i] = new Option(kvmap[0].trim(), kvmap[1].trim());
					if(kvmap[1] && selectedCityId == kvmap[1].trim()) {
						selectedIndex = i;
					}
				}
			}
		}
		if(selectedIndex != -1) {
			cityCtl.selectedIndex = selectedIndex;
		}
		if(cityCtl.disabled == true) {
			cityCtl.disabled = false;
		}
}

function changeContinent(label) {
	var continentCtl = $(label + "_continent");
	var continentId = continentCtl.options[continentCtl.selectedIndex].value;
	if(continentId != selContinent[label]) {
		selContinent[label] = continentId;
		displayCountry(label, continentId);
		displayAirport(label);
	}
}

function changeCountry(label) {
	var countryCtl = $(label + "_country");
	var countryId = countryCtl.options[countryCtl.selectedIndex].value;
	if(countryId != selCountry[label]) {	
		selCountry[label] = countryId;
		displayAirport(label, countryId);
	}
}

function removeAndDisableSelect(selCtl) {
	selCtl.disabled = true;
	var options = selCtl.options;
	var length = options.length;
	for(var i = 1; i < length; i ++) {
		selCtl.remove(1);
	}
	options[0] = new Option("loading", "");
}

function getRandomString() {
	return getDateFormatString() + getRandomNum(2);
}

// if the random string length is pow(2, digit)
function getRandomNum(digit) {
	var result = 0;
	for(var i = 1; i <= digit; i ++) {
  		result += Math.round(Math.random() * Math.pow(10, Math.pow(2, i)));
	}
	return result.toString();
}

function getDateFormatString() {
	var now = new Date();
	var year = now.getFullYear();
	var month = now.getMonth() + 1;
	var day = now.getDate();
	var hour = now.getHours();
	var min = now.getMinutes();
	var sec = now.getSeconds();
	return year.toString() + month.toString() + day.toString() + hour.toString() + min.toString() + sec.toString();
}
</script>